package com.sweets.look.ui.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sweets.look.R;


public class MarkRadioDialog {
    private final Display display;
    private final Context mContext;
    private Dialog mDialog;
    private TextView mDialogTitle;
    private RadioGroup mRadioGroup;
    private boolean showTitle;

    public MarkRadioDialog(Context context) {
        super();
        this.showTitle = false;
        this.mContext = context;
        this.display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    }

    public MarkRadioDialog builder() {
        View root = LayoutInflater.from(this.mContext).inflate(R.layout.widget_radio_dialog,
                (ViewGroup) ((Activity) this.mContext).findViewById(android.R.id.content), false);//16908290
        RelativeLayout dialog_layout = (RelativeLayout) root.findViewById(R.id.dialog_layout);
        this.mDialogTitle = (TextView) root.findViewById(R.id.dialog_title);
        this.mRadioGroup = (RadioGroup) root.findViewById(R.id.dialog_radio);
        this.mDialogTitle.setVisibility(View.GONE);
        this.mDialog = new Dialog(this.mContext, R.style.DialogTheme);
        this.mDialog.setContentView(root);

        int width = this.display.getWidth();
        int height = this.display.getHeight();
        int rWidth = width >= height ? height / 8 * 5 : width / 8 * 5;
        dialog_layout.setLayoutParams(new FrameLayout.LayoutParams(rWidth,
                ViewGroup.LayoutParams.WRAP_CONTENT));//-2
        this.mDialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
        return this;
    }

    public void dismiss() {
        if (this.mDialog != null && (this.mDialog.isShowing())) {
            this.mDialog.dismiss();
        }
    }

    public MarkRadioDialog setCancelable(boolean cancel) {
        this.mDialog.setCancelable(cancel);
        return this;
    }

    public MarkRadioDialog setCanceledOnTouchOutside(boolean cancel) {
        this.mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    private void setLayout() {
        if (this.showTitle) {
            this.mDialogTitle.setVisibility(View.VISIBLE);
        }
    }

    public MarkRadioDialog setSingleChoiceItems(int itemsId, int checkedItem, final RadioGroup.OnCheckedChangeListener
            listener) {
        String[] stringArray = this.mContext.getResources().getStringArray(itemsId);
        int i = 0;
        while (i < stringArray.length) {
            RadioButton radioButton = (RadioButton) LayoutInflater.from(this.mContext)
                    .inflate(R.layout.widget_radio,
                            (ViewGroup) ((Activity) this.mContext).findViewById(
                                    android.R.id.content), false);//16908290
            radioButton.setText(stringArray[i]);
            radioButton.setId(i);
            this.mRadioGroup.addView(radioButton, -1, -2);//-1, -2
            ++i;
        }

        this.mRadioGroup.check(checkedItem);
        this.mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (listener != null) {
                    listener.onCheckedChanged(group, checkedId);
                }

                mDialog.dismiss();
            }
        });
        return this;
    }

    public MarkRadioDialog setTitle(String title) {
        this.showTitle = true;
        this.mDialogTitle.setText(title);
        return this;
    }

    public void show() {
        this.setLayout();
        this.mDialog.show();
    }
}

